<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item label="姓名">
        <el-input
          v-model="formInline.personName"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="personName" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="sex" label="性别" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.sex == true" style="color: green">男</span>
            <span v-if="scope.row.sex == false" style="color: red">女</span>
          </template>
        </el-table-column>
        <el-table-column prop="roleId" label="角色" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.roleId == 1">超级管理员</span>
            <span v-if="scope.row.roleId == 2">普通角色 </span>
            <span v-if="scope.row.roleId == 3">管理员 </span>
            <span v-if="scope.row.roleId == 4">测试员 </span>
            <span v-if="scope.row.roleId == 5">主任 </span>
          </template>
        </el-table-column>
        <el-table-column prop="account" label="账号" align="center">
        </el-table-column>
        <el-table-column prop="password" label="密码" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="手机号" align="center">
        </el-table-column>
        <el-table-column prop="faceImage" label="人脸头像" align="center">
          <el-image :src="src" style="width: 30px; height: 30px"></el-image>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button type="text" style="color: orange" @click="upd(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" style="color: red" @click="del"
              >删除</el-button
            >
            <el-button
              type="text"
              style="color: green"
              @click="chong(scope.row)"
              >用户分配角色</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="姓名" prop="personName">
          <el-input v-model="form.personName"></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 40%">
          <el-radio-group v-model="form.sex">
            <el-radio :label="true">男</el-radio>
            <el-radio :label="false">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="账户" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="用户分配角色" :visible.sync="dialogFormVisible1">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item>
          <el-radio-group v-model="form.roleId" style="width: 500px">
            <el-radio :label="1"> 超级管理员</el-radio>
            <el-radio :label="2">普通角色</el-radio>
            <el-radio :label="3"> 管理员</el-radio>
            <el-radio :label="4">测试员</el-radio>
            <el-radio :label="5"> 主任</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun1" style="color: green">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  GetShowUserManage,
  deleteUserManage,
  updUserManage,
  GetTotaocount,
} from "@/api/sysManage/UserManage";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      src: "https://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg",
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      multipleSelection: [],
      ids: {},
      form: {
        id: "",
        personName: "",
        sex: "",
        account: "",
        password: "",
        phone: "",
        roleId: "",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        personName: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //显示
    list() {
      GetShowUserManage(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deleteUserManage(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //修改
    upd(row) {
      this.dialogFormVisible = true;
      this.form = row;
    },
    baocun() {
      updUserManage(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },

    //用户分配角色
    chong(row) {
      this.dialogFormVisible1 = true;
      this.form = row;
    },
    baocun1() {
      updUserManage(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "分配角色成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible1 = false;
        } else {
          this.$message.error("分配角色失败");
        }
      });
    },
  },
};
</script>
<style>
</style>